package com.ccl.iot.privates;

import android.content.Context;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.ccl.iot.device.IOTDimmer;
import com.ccl.iot.dialogs.NoTitleDialog;
import com.ccl.iot.R;

public class ControlDimmerPop extends NoTitleDialog {

	private Context mContext;
	private IOTDimmer mDimmer;
	private SeekBar mBar;
	
	public ControlDimmerPop(Context context, IOTDimmer dimmer) {
		super(context);
		this.mContext = context;
		this.mDimmer = dimmer;
		init();
	}

	private void init() {
		View view = View.inflate(mContext, R.layout.pop_dimmer, null);
		this.setContentView(view);
		
		mBar = (SeekBar) view.findViewById(R.id.seekBar);
		mBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				if(mDimmer != null){
					mDimmer.setDimmer(seekBar.getProgress(),true);
				}
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				mDimmer.onStartDimmer();
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				if(fromUser){
					mDimmer.setDimmer(progress, false);
				}
			}
		});
		
		initProgress();
		
		this.show();
	}
	
	private void initProgress() {
		mBar.setProgress(mDimmer == null?0:mDimmer.getDimmer());
	}

}
